package com.joywifi.android.demo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.joywifi.android.demo.R;
import com.joywifi.android.demo.adapter.RecycleViewAdapter;
import com.joywifi.android.demo.common.DemoApplication;
import com.joywifi.android.demo.entity.Item;
import com.joywifi.android.demo.utils.UiUtils;
import com.joywifi.android.demo.volley.GsonRequest;
import com.joywifi.android.demo.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MainRecycleFragment extends Fragment implements RecycleViewAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {

    private View mView;
    private RecyclerView mRecyclerView;
    private RecycleViewAdapter mRecycleViewAdapter;

    private LinearLayoutManager mLayoutManager;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private List<String> mDatas;

    private int lastVisiableItem;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragement_main_recycle, container, false);
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipe_refresh_layout);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

        initData();
        mRecycleViewAdapter = new RecycleViewAdapter(mDatas, getActivity());
        mRecycleViewAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mRecycleViewAdapter);

        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisiableItem + 1 == mRecycleViewAdapter.getItemCount()) {
                    GsonRequest<Item> gsonRequest = new GsonRequest<>("http://192.168.1.100:81/demo.json", Item.class, null,
                            new Response.Listener<Item>() {
                                @Override
                                public void onResponse(Item item) {
                                    mRecycleViewAdapter.addItemAtIndex(lastVisiableItem + 1, item.getName() + "上拉");
                                    mSwipeRefreshLayout.setRefreshing(false);
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            mSwipeRefreshLayout.setRefreshing(false);
                            UiUtils.snackbar(getView(), "加载错误,请稍后再试");
                        }
                    });
                    DemoApplication.getInstance().addToRequestQueue(gsonRequest);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisiableItem = mLayoutManager.findLastVisibleItemPosition();
            }
        });

        return mView;
    }

    @Override
    public void onItemClick(View view, int position) {
        UiUtils.snackbar(mRecyclerView, getString(R.string.item_clicked));
    }

    @Override
    public void onItemLongClick(View view, int position) {
        UiUtils.snackbar(mRecyclerView, getString(R.string.item_longclicked));
    }

    @Override
    public void onRefresh() {
        GsonRequest<Item> gsonRequest = new GsonRequest<>("http://192.168.1.100:81/demo.json", Item.class, null,
                new Response.Listener<Item>() {
                    @Override
                    public void onResponse(Item item) {
                        mRecycleViewAdapter.addItemAtIndex(0, item.getName());
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                UiUtils.snackbar(getView(), "加载错误,请稍后再试");
            }
        });
        DemoApplication.getInstance().addToRequestQueue(gsonRequest);
    }

    private void initData() {
        mDatas = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            mDatas.add("item" + i);
        }
    }
}
